import config from '@/config.js'

// 请求封装
export function request(method, url, data) {
	let promise = new Promise((resolve, reject) => {
		let postData = data;
		
		let that = this
		uni.request({
			url: config.baseUrl + url,
			data: postData,
			method: method,
			timeout: 60000, //请求超时时间
			dataType: 'json',
			header: {
				'content-type': 'application/json',
				'token': uni.getStorageSync('token')
			},
			success: function(res) {
				let codeArr = [100001, 100002, 100004, 100005]
				let token = uni.getStorageSync('token')
				if (res.data.code === 200) {
					resolve(res.data);
				} else if (codeArr.includes(res.data.code)) {
					if (token) {
						uni.removeStorageSync('token')
					}
					uni.removeStorageSync('apiToken')
					uni.redirectTo({
						url: '/login/login'
					})
				} else if (res.data.code === 500) {
					uni.$u.toast(res.data.msg)
				} else {
					uni.$u.toast(res.data.msg)
				}
			},
			fail: function(e) {
				reject('网络出错');
				uni.$u.toast('网络出错')
			}
		});
	});
	return promise;
}